// pages/user/user/identityAuth/identityAuth.js
const app = getApp();
import Api from '../../../../utils/http';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addPicList: [], //上传的图片集
    imgarr: [], //证明图片集id
    mobile: '',
    myname: '',
    codeText: '获取验证码',
    zhengLove: true,
    verifyCode: '', //验证码
    codeTime: 60, //验证码时间
    avatarUrl: '', //头像路径
    nickName: '', //用户名
    isDel: false,
    chooseViewShow: true,
  },
  //获取姓名
  getMyName: function (e) {
    var that = this;
    var myname = e.detail.value;
    // console.log(mobile)
    if (!(/^[u4E00-u9FA5]+$/.test(myname))) {
      that.setData({
        ajxtrue: false
      })
      if (myname.length === '') {
        wx.showToast({
          title: '请输入姓名',
          icon: 'success',
          duration: 2000
        })
      }
    } else {
      that.setData({
        ajxtrue: true
      })
    };
    that.setData({
      myname: myname,
    })
  },
  //获取手机号
  getMobileInput: function (e) {
    var that = this;
    var mobile = e.detail.value;
    // console.log(mobile)
    if (!(/^1[345789]\d{9}$/.test(mobile))) {
      that.setData({
        ajxtrue: false
      })
      if (mobile.length >= 11) {
        wx.showToast({
          title: '手机号有误',
          icon: 'success',
          duration: 2000
        })
      }
    } else {
      that.setData({
        ajxtrue: true
      })
    };
    that.setData({
      mobile: mobile,
    })
  },

  // 验证码输入
  getCodeInput: function (e) {
    let that = this;
    let phoneCode = e.detail.value;
    let verifyCode = that.data.verifyCode;
    console.log(verifyCode)
    that.setData({
      phoneCode: phoneCode,
      zhengLove: false,
    })
    if (phoneCode.length > 6) {
      if (phoneCode == verifyCode) {
        that.setData({
          zhengLove: true,
        })
      } else {
        that.setData({
          zhengLove: false,
        })
        wx.showModal({
          content: '输入验证码有误',
          showCancel: false,
          success: function (res) {}
        })
      }
    }
  },
  //点击获取验证码倒计时
  getVerificationCode() {
    var that = this;
    that.setData({
      disabled: true
    });
    that.getverify();
    that.changeCodeNum("true");
  },
  changeCodeNum: function () {
    let that = this,
      codeTime = that.data.codeTime;
    let clock = setInterval(() => {
      codeTime--;
      that.setData({
        codeText: codeTime + 's'
      });
      if (codeTime >= 0) {
        that.setData({
          codeTime: codeTime
        })
      } else {
        clearInterval(clock)
        that.setData({
          codeTime: 60,
          mobileStatus: false,
          codeText: '获取验证码'
        })
      }
    }, 1000)
  },
  /** 选择上传图片 */
  uploadPic: function () {
    var that = this;
    let {
      isDel,
      addPicList,
      imgarr,
    } = that.data
    if (addPicList.length >= 0) {
      that.setData({
        isDel: true
      })
    }
    // that.chooseViewShow()
    wx.chooseImage({
      count: 5, //最多选择几张图片
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        wx.showLoading({
          title: '正在上传中',
          mask: true,
        })
        let webSiteUrl = app.globalData.webSiteUrl
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片

        console.log(tempFilePaths[0])
        wx.uploadFile({
          url: webSiteUrl + '/app/Upload/upload', //接口地址
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {},
          success(res) {
            wx.hideLoading();
            var data = res.data
            data = JSON.parse(data);
            console.log(data)
              that.setData({
                addPicList: addPicList.concat(data.data),
                imgarr: imgarr.concat(data.data.id)
              })

          }
        })

        //that.chooseViewShow();
      }
    })

  },
  /** 删除图片 */
  deleteImvAddPicList: function (e) {
    var that = this;
    var addPicList = that.data.addPicList;
    var imgarr = that.data.imgarr;
    var itemIndex = e.currentTarget.dataset.index;
    addPicList.splice(itemIndex, 1);
    imgarr.splice(itemIndex, 1);
    that.setData({
      addPicList: addPicList,
      imgarr: imgarr
    })
    //判断是否隐藏选择图片
    that.chooseViewShow();
  },
  /** 是否隐藏图片选择*/
  chooseViewShow: function () {
    let {
      addPicList
    } = this.data
    if (addPicList.length >= 3) {
      this.setData({
        chooseViewShow: false
      })
    } else {
      this.setData({
        chooseViewShow: true
      })
    }
  },
 
  //请求提交反馈意见接口
  formSubmit: function (e) {
    var that = this;
    console.log(that.data)
    if (that.data.myname=='') {
      wx.showModal({
        title: '提示',
        content: "请输入姓名",
        showCancel: false,
        confirmColor: "#db3c40"
      })
    } else if (!/^1[3456789]\d{9}$/.test(e.detail.value.mobile)) {
      wx.showModal({
        title: "提示",
        content: "请输入正确的联系方式",
        showCancel: false,
        confirmColor: "#db3c40"
      })
    } else if (e.detail.value.phoneCode === that.data.verifyCode) {
      wx.showModal({
        title: "提示",
        content: "请输入正确的验证码",
        showCancel: false,
        confirmColor: "#db3c40"
      })
    }else if(that.data.addPicList.length==0){
      wx.showModal({
        title: "提示",
        content: "身份证图片为空",
        showCancel: false,
        confirmColor: "#db3c40"
      })
    } else {
      that.getVerification(e);
      
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  
  //单位、部门认证
  getVerification:function(e){
    var that=this;
    let uid=wx.getStorageSync('userid')
    var ID_card=that.data.imgarr;
    var param={
      uid:uid.id,
      user_type:uid.user_type,
      name:that.data.myname.toString(),
      mobile:that.data.mobile.toString(),
      yzm:that.data.phoneCode,
      ID_card:ID_card.toString(),
    }
    console.log(param);
    Api.ajax('/app/Zmanage/verification',param).then(res=>{
      console.log(res)
      if (res.code==200) {
        wx.showToast({
          title: "身份认证成功",
          icon: 'success',
          duration: 1000,
          mask: false,
          success: function () {
            wx.navigateBack({
              detail: 1
            })
          },
        })
      }else if (res.code==201){
        wx.showToast({
          title: "验证码错误",
          icon: 'none',
          duration: 2000,
          mask: false,
        })
      }else{
        wx.showToast({
          title: "请检查信息是否正确",
          icon: 'none',
          duration: 2000,
          mask: false,
        })
      }
    })
  },
  //获取验证码接口
  getverify: function () {
    var that = this;
    var param = {
      mobile: that.data.mobile,
      type: 4,
    }
    console.log(param)
    Api.ajax("/app/Zaccount/yzm", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        wx.showToast({
          title: '获取成功', //这里成功
          icon: 'success',
          duration: 1000
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})